package com.luzhikun.jsptest.myjsp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: SearchStudentsServlet
 * Description:
 * date: 2021/4/27 12:28
 *
 * @author luzhikun
 * @since JDK 1.8
 */
public class SearchStudentsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //设置字符集
        resp.setContentType("text/html;charset=UTF-8");
        //1.得到IDao对象
        StudentsIDao studentsIDao = new StudentsIDao();
        //2.获取到所有的学生集合
//        List<Students> student = studentsIDao.getStudent("吴");
        //3，创建输出流
//        PrintWriter writer = resp.getWriter();
//        for (Students students:student) {
//            writer.write(students.getId());
//            writer.write(students.getName());
//            writer.write(students.getAge());
//            writer.write(students.getSex());
//        }
        List<Students> list = new ArrayList<>();
        //使用for循环做模拟数据
        for (int i = 0; i < 10; i++) {
            int t = i + 1;
            list.add(new Students(t, "name" + i, 10 + i, "男"));

        }
//        writer.write(student.size());
        //将学生对象设置到域属性中
        req.setAttribute("student", list);
        //请求转发
        //斜线表示当前工程下的web目录
        //分析：
        /*
         * 首先 加了斜线
         * 然后 后台
         * 然后表示当前工程下
         *
         * */
        //请求转发到showStudent.jsp页面
        req.getRequestDispatcher("/test/showStudents.jsp").forward(req, resp);


    }
}
